import 'dart:io';

import 'package:basic/information_pages/informatin.dart';
import 'package:flutter/material.dart';

import 'login.dart';
import 'main_page.dart';
import 'sign_up.dart';
import 'forget_password.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  realRunApp();
}

class SpUtil {
  static SharedPreferences prefs;
  static Future<bool> getUserName() async {
    prefs = await SharedPreferences.getInstance();
    return true;
  }
}

void realRunApp() async {
  await SpUtil.getUserName();
  runApp(Router());
}

class Router extends StatelessWidget {
  final routes = {
    '/': (context) => Login(),
    '/login': (context) => Login(),
    '/main': (context, {arguments}) => MainPage(arguments: arguments),
    '/sign_up': (context) => SignUp(),
    '/forget_password': (context) => ForgetPassword(),
    '/infor': (context, {arguments}) => Infor(arguments: arguments),
  };

  @override
  Widget build(BuildContext context) {
    String initialRoute = '/';
    int id = SpUtil.prefs.getInt('id');
    if (id != null && id != 0) {
      initialRoute = '/main';
      print("888888888888888888888888888888888888888");
    }
    return MaterialApp(
      debugShowCheckedModeBanner: false, //去掉debug
      initialRoute: initialRoute,
      //routes: routes,
      onGenerateRoute: (RouteSettings settings) {
        //统一处理
        final String name = settings.name;
        final Function pageContentBuilder = this.routes[name];
        if (pageContentBuilder != null) {
          if (settings.arguments != null) {
            final Route route = MaterialPageRoute(
                builder: (context) =>
                    pageContentBuilder(context, arguments: settings.arguments));
            return route;
          } else {
            final Route route = MaterialPageRoute(
                builder: (context) => pageContentBuilder(context));
            return route;
          }
        }
        return null;
      },
    );
  }
}
